package com.edu.base.common.Interceptor;

import com.alibaba.fastjson.JSON;
import com.edu.base.rest.service.AppTokenService;
import com.edu.base.ums.member.domain.ClsMember;
import com.edu.utils.UserContextUtil;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.AppLoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * app端接口token验证
 */
@Component
public class AppLoginAuthInterceptor implements HandlerInterceptor {

    // 令牌自定义标识
    @Value("${token.appheader}")
    private String appheader;

    // 令牌自定义标识
    @Value("${token.enableAuth}")
    private boolean enableAuth;


    @Autowired
    private AppTokenService appTokenService;


    /**
     * 鉴权的拦截器
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // app接口验证开关
        if(!enableAuth){
            return true;
        }

        try {
            //String appToken =  request.getHeader(appheader);
            ClsMember clsMember = appTokenService.getAppMember(request);
            if(StringUtils.isNotNull(clsMember)){
                // 将用户信息存入ThreadLocal
                AppLoginUser appLoginUser = new AppLoginUser();
                BeanUtils.copyProperties(clsMember, appLoginUser);
                UserContextUtil.setUser(appLoginUser);
                return true;
            }else{
                String msg = StringUtils.format("请求访问：{}，认证失败，无法访问系统资源", request.getRequestURI());
                ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(HttpStatus.UNAUTHORIZED, msg)));
                return false;
            }
        }catch(Exception e){
            String msg = StringUtils.format("请求访问：{}，认证失败，无法访问系统资源", request.getRequestURI());
            ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(HttpStatus.UNAUTHORIZED, msg)));
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle++++++++++++++");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion++++++++++++++");
    }





}
